<?php

include "client_functions.php";


function usage($error){
	if($error == 0){
		echo "Usage:\n";
		echo "*****************************\n";
		echo "PUT usage\n";
		echo "----------\n";
		echo "PUT is used to upload a file from the local client machine to the SFTP server.\n";
		echo "PUT server_path client_path \n";
		echo "server_path : the full path of the remote directory to upload to, the path must include the file name.\n";
		echo "client_path : the full path of the client file to upload.\n";
		echo "---------------------------------------\n";
		echo "GET usage\n";
		echo "----------\n";
		echo "GET is used to downlaod a file from the remote SFTP server to the client machine.\n";
		echo "GET server_path client_path \n";
		echo "server_path : the full path of the remote server file to download.\n";
		echo "client_path : the full path of the client directory to download to, the path must include the file name.\n";
		echo "---------------------------------------\n";
		echo "LIST usage\n";
		echo "----------\n";
		echo "LIST lists the contents of a remote SFTP server directory.\n";
		echo "LIST server_path \n";
		echo "server_path : the full path of the remote server file to list its contents.\n";
		echo "---------------------------------------\n";
		echo "DCON usage\n";
		echo "----------\n";
		echo "DCON connects directly to the SFTP server bypassing the Name Server.\n";
		echo "DCON server_ip server_port\n";
		echo "server_name : the IP address of the SFTP server you want to directly connect to.\n";
		echo "server_port : the port number of the SFTP server you want to directly connect to.\n";
		echo "---------------------------------------\n";
		echo "CON usage\n";
		echo "--------\n";
		echo "CON connects to an SFTP server by resolving the Server Name first from the DNS.\n";
		echo "CON server_name\n";
		echo "server_name : the name of the SFTP server you want to connect to.\n";
		echo "---------------------------------------\n";
		echo "Type DISC to disconnect from the remote SFTP server.\n";
		echo "---------------------------------------\n";
		echo "Type CLOSE to close the pipe between the php and the client process.\n";
		echo "***************************************************************\n";		
	}
	
	else if($error == 1){
		echo "PUT usage\n";
		echo "----------\n";
		echo "PUT is used to upload a file from the local client machine to the SFTP server.\n";
		echo "PUT server_path client_path \n";
		echo "server_path : the full path of the remote directory to upload to, the path must include the file name.\n";
		echo "client_path : the full path of the client file to upload.\n";
		echo "---------------------------------------\n";
	}
	else if($error == 2){
		echo "GET usage\n";
		echo "----------\n";
		echo "GET is used to downlaod a file from the remote SFTP server to the client machine.\n";
		echo "GET server_path client_path \n";
		echo "server_path : the full path of the remote server file to download.\n";
		echo "client_path : the full path of the client directory to download to, the path must include the file name.\n";
		echo "---------------------------------------\n";
	}
	else if($error == 3){
		echo "LIST usage\n";
		echo "----------\n";
		echo "LIST lists the contents of a remote SFTP server directory.\n";
		echo "LIST server_path \n";
		echo "server_path : the full path of the remote server file to list its contents.\n";
		echo "---------------------------------------\n";
	}
	else if($error == 4){
		echo "DCON usage\n";
		echo "----------\n";
		echo "DCON connects directly to the SFTP server bypassing the Name Server.\n";
		echo "DCON server_ip server_port\n";
		echo "server_name : the IP address of the SFTP server you want to directly connect to.\n";
		echo "server_port : the port number of the SFTP server you want to directly connect to.\n";
		echo "---------------------------------------\n";
	}
	else if($error == 5){
		echo "CON usage\n";
		echo "--------\n";
		echo "CON connects to an SFTP server by resolving the Server Name first from the DNS.\n";
		echo "CON server_name\n";
		echo "server_name : the name of the SFTP server you want to connect to.\n";
		echo "---------------------------------------\n";
	}	
}

//get the arguments now 
if($argv[1] == "PUT"){
	if(empty($argv[2]) || empty($argv[3])) 
		usage(1);
	else{ 
		$res = put($argv[2], $argv[3]);
		if($res == "DONE")
			echo "File uploaded sucessfully....\n";
		else 
			echo "\n Make sure your configuartions are correct ...\n";
	}
}
else if ($argv[1] == "GET"){
	if(empty($argv[2]) || empty($argv[3])) 
		usage(2);
	else{ 
		$res = get($argv[2], $argv[3]);
		if($res == "DONE")
			echo "File downloaded sucessfully....\n";
		else echo "\n Make sure your configuartions are correct ...\n";
	}
}
else if ($argv[1] == "LIST"){
	if(empty($argv[2])) 
		usage(3);
	else{
		lis($argv[2]);
	}
}
else if ($argv[1] == "DCON"){
	if(empty($argv[2]) || empty($argv[3]) ) 
		usage(4);
	else{
		$res = directConnect($argv[2], $argv[3]);
		if($res == "DONE")
			echo "Connected to SFTP server successfully....\n";
		else echo "\n Make sure your configuartions are correct ...\n";
	}
}
else if ($argv[1] == "CON"){
	if(empty($argv[2])) 
		usage(5);
	else{ 
		$res = connect($argv[2]);
		if($res == "DONE")
			echo "Connected to SFTP server successfully....\n";
		else echo "\n Make sure your configuartions are correct ...\n";		
	}
}
else if ($argv[1] == "DISC"){
	$res = disconnect();
	if($res == "DONE")
			echo "Disconnected from SFTP server successfully....\n";
		else echo "\n Make sure your configuartions are correct ...\n";
}
else if ($argv[1] == "CLOSE"){
	$res = close();
	if($res == "DONE")
		echo "Connection closed ...\n";	
}
else usage(0);

?>
